package com.example.cinema.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.cinema.R;
import com.example.cinema.bindingdata.CarouseData;
import com.example.cinema.drawview.RoundJiao;

import java.util.ArrayList;
import java.util.List;

public class CarouselAdapter extends PagerAdapter {

    private List<ImageView> dataimg = new ArrayList<>();
    public CarouselAdapter(List<CarouseData> data, Context context) {
        for (int i=0;i<data.size();i++)
        {
            RoundJiao view = new RoundJiao(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.leftMargin = 5;
            params.rightMargin = 5;
            view.setLayoutParams(params);
            view.setPadding(5,0,5,0);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setTag(R.integer.ctype,data.get(i).type);
            view.setTag(R.string.curl,data.get(i).address);
            view.setTag(R.string.cmsg,data.get(i).news);
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.defaut_img)
                    .error(R.mipmap.defaut_img)
                    .fallback(R.mipmap.defaut_img);
            Glide.with(view).load(data.get(i).imgurl).apply(options).into((ImageView) view);
            int finalI = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(itemClickListener!=null)
                    {
                        itemClickListener.itemClick(data.get(finalI), finalI);
                    }
                }
            });

            dataimg.add(view);
        }
    }

    @Override
    public int getCount() {
        return dataimg.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        container.addView(dataimg.get(position));
        return dataimg.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(dataimg.get(position));
    }


    public interface  OnPageItemClickListener{
        void itemClick(CarouseData cdata,int positon);
    }
    private OnPageItemClickListener itemClickListener;
    public void setOnPageItemClickListener(OnPageItemClickListener listener)
    {
        itemClickListener = listener;
    }
}
